package com.itheima.controller;

import com.itheima.common.util.Result;
import com.itheima.jopo.Emp;
import com.itheima.service.EmpService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * EmpController
 *
 * @author liliudong
 * @version 1.0
 * @description
 * @date 2023/4/28 12:59
 */
@RestController
public class EmpController {

    private final EmpService empService;

    /**
     * 这里采用了构造器注入法
     *
     * @param empService
     */
    public EmpController(EmpService empService) {
        this.empService = empService;
    }

    /**
     * 新增一个EMP员工
     *
     * @param emp emp
     * @return void
     */
    @RequestMapping("/add")
    public Result addUser(@RequestBody Emp emp) {
        if (empService.existByUsername(emp.getUsername())) {
            return Result.error("员工已经存在，请勿重复添加");
        }
        empService.saveOne(emp);
        return Result.success();
    }

    /**
     * 根据ID删除一个员工
     *
     * @param id id
     * @return void
     */
    @RequestMapping("/del/{id}")
    public Result deleteById(@PathVariable String id) {
        empService.removeById(id);
        return Result.success();
    }

    /**
     * 根据ID更新一个员工信息
     *
     * @param emp emp
     * @return void
     */
    @RequestMapping("/updateById")
    public Result updateById(@RequestBody Emp emp) {
        empService.updateById(emp);
        return Result.success();

    }

    /**
     * 查询所有员工信息
     *
     * @return List<Emp>
     */
    @RequestMapping("/list")
    public Result list() {
        List<Emp> list = empService.queryList();

        return Result.success(list);

    }

    /**
     * 根据ID查询用户详情信息
     *
     * @param id id
     * @return Emp
     */
    @RequestMapping("/info/{id}")
    public Result infoById(@PathVariable String id) {
        Emp emp = empService.queryById(id);
        return Result.success(emp);

    }

}
